<?php

declare(strict_types=1);

namespace App\Validations;

use App\Repositories\RoleRepository;

/**
 * Define las reglas de validación de los usuarios.
 */
class UserValidation extends BaseValidation
{
    private array $roles = [];

    public function getAllRules(): array
    {
        return [
            'id' => ['guidv4'],
            'email' => ['valid_email', 'between_len' => [1, 128]],
            'username' => ['alpha_numeric_dash', 'between_len' => [1, 64]],
            'role_id' => ['guidv4', 'contains_list' => $this->getRoles()],
            'fullname' => ['alpha_space', 'between_len' => [1, 128]],
            'password' => ['regex' => '/^\S+$/', 'between_len' => [8, 32]],
            'is_active' => ['boolean'],
        ];
    }

    public function getAllFilters(): array
    {
        return [
            'fullname' => ['trim'],
            'email' => ['lower_case'],
            'is_active' => ['boolean'],
        ];
    }

    /**
     * Establece los roles para las validaciones.
     */
    private function setRoles(string $column): void
    {
        $this->roles = (new RoleRepository)->getColumValues($column);
    }

    /**
     * Obtiene los roles para las validaciones.
     */
    private function getRoles(): array
    {
        if (empty($this->roles)) {
            $this->setRoles('id');
        }

        return $this->roles;
    }
}
